/**
 * 
 */
package com.base.myproject.client.busi.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseStoreEvent;
import com.base.myproject.client.busi.BaseStoreListener;
import com.base.myproject.client.busi.ColumnInfo;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.ClientEnv.SystemInfo;
import com.base.myproject.client.busi.ccode.NewCCode;
import com.base.myproject.client.busi.print.Printer;
import com.base.myproject.client.busi.system.BcodeChecker;
import com.base.myproject.client.busi.system.BcodeSync;
import com.base.myproject.client.busi.system.PrinterSync;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.tools.Debug;
import com.base.myproject.client.ui.BaseBusiLeftPanel;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.PrintDialog;
import com.base.myproject.client.ui.celleditor.BaseGridEditor;
import com.base.myproject.client.ui.celleditor.CellEditorFactory;
import com.base.myproject.client.ui.celleditor.GridEditor;
import com.base.myproject.client.ui.codenamemap.CodeNameMapFactory;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.StoreEvent;
import com.extjs.gxt.ui.client.store.StoreListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.AggregationRowConfig;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.SummaryType;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.tips.ToolTipConfig;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.xml.client.Element;

/**
 * 会员正在接受服务的录入界面，主要做查询会员号， 登记正在接受服务的会员和接受服务内容
 * 
 * @author zgw 2009-10-7 下午06:40:13 javazgw@gmail.com
 */
public class ServiceListing extends BaseJDBCPanel implements
		Listener<BaseEvent> ,BaseStoreListener,Printer,AsyncCallback<Associator> {

	BaseStore<BusiObject> store = null;
	BaseStore<BusiObject> s_service_po_g_store ;
	final NumberFormat currency = NumberFormat.getCurrencyFormat();
	final NumberFormat number = NumberFormat.getFormat("0.00");
	Button endbutton;
	Button paidbutton;
	Button startbutton;
	Button printer;
	Button associatorbutton;
	Button changedpo;
	Button addfinance;
	Button associate; //交接按钮
	Button newccode;
	Button addnew; //添加新单据按钮
	NumberField Accounts ;
	NumberField Paid ;
	DateField servicestartdate ;
	DateField serviceenddate;
	DateField createdate ;
	NumberField peoplenum ;
	BaseGrid basegrid;
	BaseGrid s_service_po_g_grid ;
	ContentPanel service_panel ;

	VerticalPanel vpanel;
	ColumnConfig bcode;
	
	BaseStore bcode_rander_store;
	
	TextField<String> s_cardnothis;
	//BaseComboBoxForm<ModelData> ccode ;
	TextField<String> ccode;
	TextField<String> roomno ;
	TextField<String>  cname_t, s_mobil; 
	Label aslabel;
	CheckBox  check_isas = new CheckBox();  
	
	BaseFormPanel bfp;
	
	NumberField html=new NumberField();
//	Associator associator=null;
	
	FieldSet fieldSet_isas ;
	Button getassoc_button ;
	public ServiceListing() {
		
		super();
		init();
		// this.setScrollMode(Scroll.AUTO);
	}

	public void init() {
		
		//getToolbarll().add(e)
		
		initRenderStore();
		busi = SystemMessage.busi[8];
		this.setWidth("1024px");
		//this.setHeight("720px");
		this.setAutoHeight(true);
		
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 5, 0, 0));
		this.add(initWeigetx(), centerData);

		registerMainStore(store);
		registerStore(store);

		this.setBusino(busi);
		setdefault();
		store.addBaseStoreListener(BaseStoreEvent.EventType.ROW_CHANGED, this);
		

		
	}
	public void complete()
	{
		//工具栏菜单
		getToolbarll().add(addnew);
		getToolbarll().add(paidbutton);
		getToolbarll().add(printer);
		//getToolbarll().add(associatorbutton);
		//getToolbarll().add(addfinance);
		getToolbarll().add(associate);
		getToolbarll().add(newccode);
	//	initToolBarItems();
		super.complete();
	}
	/**
	 * 加载渲染的码名映射，在节目初始化之前完成否则界面显示的是码
	 */
	void initRenderStore()
	{



			ModelType combotype2 = new ModelType();
			combotype2.setRoot("records");
			combotype2.setRecordName("record");
			combotype2.setTotalName("totle");
			// combotype.addField("code", "service");
			combotype2.addField("bcode");
			combotype2.addField("bname");
			
			StoreInfo comboinfo2 = new StoreInfo();
			comboinfo2.setTablename("bcode");
			comboinfo2.setSql("select bcode,bname from bcode");
			
			 bcode_rander_store = new BaseStore(combotype2, comboinfo2);
			 
			 //装入ccode的store
			 CodeNameMapFactory
				.getBaseccodestore();
			
	}

	private void setdefault() {
		/*
		 * if(store!=null) { store.addDefault("busicode", busi);
		 * store.addDefault("bcode", Env.getEnvbcode()); }
		 */
		

		
		
		store.addDefault("bcode", Env.getEnvbcode()); 

		store.addDefault("busicode", busi);
		
	}
	private Widget initWeigetx()
	{
		vpanel = new VerticalPanel();
		HorizontalPanel h= new HorizontalPanel();
		vpanel.setHorizontalAlign(HorizontalAlignment.LEFT);
		h.setVerticalAlign(VerticalAlignment.TOP);
		
		
		ContentPanel cpleft = CreateCcodeLeftPanel();
		 changedpo = new Button("修改此单据");
		cpleft.setTopComponent(changedpo);
		changedpo.addListener(Events.Select, this);
		
		 CreateButtonGroup();
		 h.add(cpleft);
		 vpanel.add(createService());
		 vpanel.add(createServiceItem());
			
		
			h.add(vpanel);
			h.setSpacing(5);
			return h;
			
		
	}
//	public ContentPanel initWeiget() {
//		ContentPanel panel = new ContentPanel();
//		panel.setHeaderVisible(false);
//		panel.setLayout(new BorderLayout());
//		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 350);
//		westData.setSplit(true);
//		westData.setCollapsible(true);
//		westData.setMargins(new Margins(10));
//		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
//		centerData.setMargins(new Margins(10));
//		
//		ContentPanel cpleft = CreateCcodeLeftPanel();
//		 changedpo = new Button("修改此单据");
//		cpleft.setTopComponent(changedpo);
//		changedpo.addListener(Events.Select, this);
//		panel.add(cpleft, westData);
//		//CreateCcodeLeftPanel();
//		 vpanel = new VerticalPanel();
////		 AssociatorPanel associatorpanel = new AssociatorPanel();
//		 CreateButtonGroup();
//	//	 vpanel.add(CreateButtonGroup());
////		 vpanel.add(associatorpanel);
//		vpanel.add(createService());
//		vpanel.add(createServiceItem());
//		
//		panel.add(vpanel, centerData);
//		//panel.setAutoWidth(true);
//		panel.setHeight("480");
//		//----------
//		//panel.add(vpanel, westData);
//		//BaseMainPanel.getInstance().setHeight("760");
//		return panel;
//	}

	public ContentPanel CreateCcodeLeftPanel() {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		ColumnConfig icode = new ColumnConfig("icode", "单号", 40);
		
		//columns.add(icode);
//		columns.add(new ColumnConfig("ccode	", "ccode", 50));
//		columns.add(new ColumnConfig("roomno", "roomno", 50));
		ColumnConfig busistatus = new ColumnConfig("busistatus", "状态", 40);
		
		GridCellRenderer<ModelData>	renderer = new GridCellRenderer<ModelData>() {

		      public String render(ModelData model, String property, ColumnData d, int rowIndex,
		          int colIndex, ListStore<ModelData> store, Grid<ModelData> grid) {

		       if(model.get(property)==null)
		        return "" ;

		       if(model.get(property).equals("524288"))
		           return "" + ("<img src='images/baseimage/money_yen.png' />" );
//		       else if(model.get(property).equals("2"))
//		       	   return "" + ("<img src='../images/baseimage/icon-complete.gif' />" );
		          return "" + ("<img src='images/baseimage/cut.png' />" );
		      // return SystemMessage.getstatus(model.get(property)+"");
		      }
		    };
		    busistatus.setRenderer(renderer) ;
		columns.add(busistatus);
		columns.add(new ColumnConfig("cname", "会员名", 50));
		columns.add(new ColumnConfig("s_mobil", "电话", 80));
		columns.add(new ColumnConfig("createdate", "日期", 100));
/*		ColumnConfig servicestartdate = new ColumnConfig("servicestartdate", "servicestartdate", 50);

		columns.add(servicestartdate);*/
		//ColumnModel cm = new ColumnModel(columns);

		// --set modetype
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.addField("busicode");
		type.addField("bcode");
		type.addField("ccode");
		type.addField("busistatus");
		type.addField("vprepare");
		type.addField("predate");
		type.addField("modifydate");
		type.addField("codename");
		type.addField("odate");
		type.addField("mark");

		type.addField("peoplenum");
		type.addField("Accounts");
		type.addField("Paid");
		type.addField("servicestartdate");
		type.addField("serviceenddate");
		type.addField("roomno");
		type.addField("s_cardno");
		//type.addField("check_isas");
		type.addField("createdate");
		
		type.addField("cname");
		type.addField("s_mobil");
		type.addField("lastfiannce");
		
		
		type.getField("Accounts").setType(Double.class);
		type.getField("Paid").setType(Double.class);
		type.getField("peoplenum").setType(Integer.class);
		type.getField("servicestartdate").setType(Date.class);
		type.getField("serviceenddate").setType(Date.class);
		//type.getField("check_isas").setType(Boolean.class);
		type.getField("lastfiannce").setType(Double.class);
		
		StoreInfo info = new StoreInfo();
		info.setTablename("s_service_po");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from s_service_po where DATEDIFF(day, getdate(), createdate)=0 and bcode='"+Env.getEnvbcode()+"'");
		// info.setKeyname(new String[] { "codename" });

		BaseBusiLeftPanel<BusiObject> basbusileftpanel = new BaseBusiLeftPanel<BusiObject>(columns,
				type, info)
		{
			protected boolean Select(Store<BusiObject> store,
					BusiObject parent, BusiObject record, String property,
					String filter) {
				for (int i = 0; i < type.getFieldCount(); i++) {
					
					Object name = record.get(type.getField(i).getName());
				//状态用了码名映射，所以要对名字进行搜索
					if(type.getField(i).getName().equals("busistatus"))
					{
						if(name!=null && SystemMessage.getstatus(name+"").indexOf(filter.toLowerCase()) !=-1)
						{
							return true;
						}
						
					}
					
					else if (name!=null&&name.toString().indexOf(filter.toLowerCase()) != -1) {
						return true;
					}
				}
				return false;
			}
			
			public boolean candeleterecord()
			{
				if(this.getStore().getCount()<=0)
				{
					return false;
				}
				if(this.getStore().getValue("busistatus").equals("524288"))
				{
				MessageBox.alert("警告", "不能被删除，因为已经被收银。",	null);
				return false;
				
				}
				return true;
			}
			
			public void doBeforeRowChangeGrid()
			{
				if(store.isIschanged() || s_service_po_g_store.isIschanged() )
				{
					store.trySave();
				}
				
			}
			
			public void setdefault()
			{
				DateTimeFormat activeFormat = DateTimeFormat
				.getFormat("yyyy-M-d H:mm:ss");
				
//				store.addDefault("createdate",activeFormat.format(new Date()));
				store.addDefault("createdate",new Date());
				
			}
		};
		store = basbusileftpanel.getStore();
		store.addDefault("busistatus","128");
		store.applyFilters("524288");
		basbusileftpanel.getGrid().setAutoExpandColumn("createdate");
		basbusileftpanel.getGrid().setWidth("320");
		basegrid = basbusileftpanel.getGrid();
		setMaincomponent(basegrid);
		return basbusileftpanel.getPanel();
	}

	void CreateButtonGroup()
	{
		
		addnew =  new Button("添加新收银");
		addnew.addListener(Events.Select, this);
		addnew.setId("add_new_service");
		addnew.setIconStyle("addrecord16");
		
		
		startbutton = new Button("开始服务");
		startbutton.addListener(Events.Select, this);
		startbutton.setId("start_service");
		startbutton.setIconStyle("time_end");
		
		endbutton = new Button("结束服务");
		endbutton.setId("time_start");
		endbutton.addListener(Events.Select, this);
		endbutton.setIconStyle("time_start");
		
		
		paidbutton = new Button("收银");
		paidbutton.setId("money_yen");
		paidbutton.addListener(Events.Select, this);
		paidbutton.setIconStyle("money_yen");
		
		printer   = new Button("打印");
		//printer.setStyleName("printer");
		printer.setIconStyle("printer");
		//printer.setId("printer");
		printer.addListener(Events.Select, this);
		
		associatorbutton= new Button("会员");
		//printer.setStyleName("printer");
		associatorbutton.setIconStyle("associator");
	//associatorbutton.setId("end_service");
		associatorbutton.addListener(Events.Select, this);
		ToolTipConfig ttc=new ToolTipConfig("提示","查询会员信息");
		
		
		associatorbutton.setToolTip(ttc);
		
		
		
		addfinance = new Button("会员充值");
		
		addfinance.addListener(Events.Select, this);
		addfinance.setIconStyle("money_yen");
		
		associate= new Button("打印交接单");
		
		associate.addListener(Events.Select, this);
		associate.setIconStyle("time_end");
		
		
		newccode = new Button("添加新会员");
		newccode.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE
				.contact24()));
		newccode.addListener(Events.Select, this);
		//associatorbutton.getToolTip().show();
		
//		HorizontalPanel hpanel = new HorizontalPanel();
//		//hpanel.add(startbutton);
//		//hpanel.add(endbutton);
//		hpanel.add(paidbutton);
//		hpanel.add(printer);
//		hpanel.add(associatorbutton);
//		hpanel.add(addfinance);
//		return hpanel;
	}
	/**
	 * 头部界面，主界面
	 */
	protected ContentPanel createService() {
		 service_panel = new ContentPanel();
		 
		 bfp = new BaseFormPanel(store);
		
		TableLayout tl = new TableLayout(4);
		tl.setWidth("600");
		
		bfp.setLayout(tl);
		bfp.setHeaderVisible(false);
		 
//		BaseFormPanel leftpanel = new BaseFormPanel(store);
//		leftpanel.setLayout(new FormLayout());
//		FormData fd = new FormData(200, 30);
//		leftpanel.setHeaderVisible(false);
//		leftpanel.setBodyBorder(false);
//
//		BaseFormPanel rightpanel = new BaseFormPanel(store);
//		rightpanel.setBodyBorder(false);
//		rightpanel.setHeaderVisible(false);

		 peoplenum = new NumberField();
		peoplenum.setName("peoplenum");
		peoplenum.setAllowBlank(false);
		peoplenum.setFieldLabel("服务人数");

		
		 createdate = new DateField();
			createdate.setName("createdate");
		createdate.setAllowBlank(false);
		createdate.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-M-d H:mm:ss"));  
		createdate.setFieldLabel("建立日期");
		createdate.setVisible(false);
		
		
		servicestartdate = new DateField();
		servicestartdate.setName("servicestartdate");
		servicestartdate.setAllowBlank(false);
		servicestartdate.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-M-d H:mm:ss"));  
		servicestartdate.setFieldLabel("服务开始时间");
		
		serviceenddate = new DateField();
		serviceenddate.setVisible(false);
		serviceenddate.setName("serviceenddate");
	//	serviceenddate.setPropertyEditor(propertyEditor)
		//n.setPropertyEditorType(Integer.class);
		serviceenddate.setFormatValue(true);
		//serviceenddate.setAllowBlank(false);
		serviceenddate.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-M-d H:mm:ss"));  
		serviceenddate.setFieldLabel("服务结束时间");
		
		 roomno = new TextField<String>();
		roomno.setName("roomno");
	//	roomno.setAllowBlank(false);
		roomno.setFieldLabel("房间号");

		Accounts = new NumberField();
		Accounts.setName("Accounts");
		Accounts.setAllowBlank(false);
//		Accounts.getPropertyEditor().set
//		Accounts.setPropertyEditor(propertyEditor)
//		setPropertyEditorType(Integer.class);
	//	Accounts.getPropertyEditor().
		Accounts.setFieldLabel("应收金额");
		Accounts.setAllowBlank(true);
		 Paid = new NumberField();
		Paid.setName("Paid");
		Paid.setAllowBlank(false);
		Paid.setFieldLabel("实收金额");
		Paid.addListener(Events.Blur, new Listener(){

			@Override
			public void handleEvent(BaseEvent be) {
				  GWT.log("Paid blur date = " +new Date(), null);
				
			}
			
		});
		
		TextField<String> icode = new TextField<String>();
		icode.setName("icode");
		icode.setAllowBlank(false);
		icode.setEnabled(false);
		icode.setFieldLabel("编号");

//		ModelType type = new ModelType();
//		type.setRoot("records");
//		type.setRecordName("record");
//
//		type.addField("code");
//
//		type.addField("name");
//
//		StoreInfo info = new StoreInfo();
//
//		info.setTablename("ccode");
//		info.setSql("select ccode as code,cname as name from ccode");
//		BaseStore<BaseModelData> ccodestore = new BaseStore<BaseModelData>(
//				type, info);
//		ccode = new BaseComboBoxForm("code",
//				"name", ccodestore);
		ccode = new TextField<String>();
		ccode.setFieldLabel("客户");
		ccode.setName("ccode");
		ccode.setVisible(false);
		//ccode.setAllowBlank(false);
		
		
		 s_cardnothis = new TextField<String>();
		s_cardnothis.setName("s_cardno");
	//	roomno.setAllowBlank(false);
		s_cardnothis.setFieldLabel("会员卡号");
		
		
		cname_t = new TextField<String>();
		cname_t.setName("cname");
		// roomno.setAllowBlank(false);
		cname_t.setFieldLabel("会员名字");

		s_mobil = new TextField<String>();
		s_mobil.setName("s_mobil");
		// roomno.setAllowBlank(false);
		s_mobil.setFieldLabel("会员电话");
		// --------------
		// html = new NumberField();
		 html.setName("lastfiannce");
		// html.setAllowBlank(false);
		 html.setFieldLabel("余额");
		
		
		//zgw check_isas.setBoxLabel("使用会员卡");  
		
		//zgw check_isas.setName("check_isas");
		// --------------

//		leftpanel.add(icode);
//		rightpanel.add(ccode);
//		leftpanel.add(peoplenum);
//		rightpanel.add(roomno);
//		leftpanel.add(servicestartdate);
//		rightpanel.add(serviceenddate);
//
//		leftpanel.add(Accounts);
//		rightpanel.add(Paid);
		
		TableData bfp_td = new TableData();
		bfp_td.setColspan(1);
		bfp_td.setRowspan(1);
		// 对齐方式
		bfp_td.setHorizontalAlign(HorizontalAlignment.LEFT);
		bfp_td.setVerticalAlign(VerticalAlignment.TOP);
		bfp_td.setPadding(5);
		
		
		TableData bfp_td_set = new TableData();
		bfp_td_set.setColspan(4);
		bfp_td_set.setRowspan(1);
		// 对齐方式
		bfp_td_set.setHorizontalAlign(HorizontalAlignment.LEFT);
		bfp_td_set.setVerticalAlign(VerticalAlignment.TOP);
		bfp_td_set.setPadding(5);
		
		
		//bfp.add(new Label("使用会员卡:"),bfp_td);
		//----------------------------------------------start
		TableData td_isas = new TableData();
		td_isas.setColspan(1);
		td_isas.setRowspan(1);
		// 对齐方式
		td_isas.setHorizontalAlign(HorizontalAlignment.LEFT);
		td_isas.setVerticalAlign(VerticalAlignment.TOP);
		
		
		TableData td_isas2 = new TableData();
		td_isas2.setColspan(2);
		td_isas2.setRowspan(1);
		// 对齐方式
		td_isas2.setHorizontalAlign(HorizontalAlignment.RIGHT);
		td_isas2.setVerticalAlign(VerticalAlignment.TOP);

		 fieldSet_isas = new FieldSet();  
		fieldSet_isas.setHeading("会员信息");  
	//	fieldSet_isas.setCheckboxToggle(true);  
		// zgw 2009-12-30 fieldSet_isas.setCollapsible(true);
		//fieldSet_isas.setExpanded(true);
		fieldSet_isas.setLayout(new TableLayout(5));
		
	  //  aslabel = new Label("会员卡号:");
	    fieldSet_isas.add(new Label("会员卡号:"),td_isas);
	    fieldSet_isas.add(s_cardnothis,td_isas);
	  //  fieldSet_isas.add( new Label("会员码:"), td_isas);
	  //  fieldSet_isas.add(ccode, td_isas); 
	    fieldSet_isas.add( new Label("会员名:"), td_isas);
	    fieldSet_isas.add(cname_t, td_isas); 
	     getassoc_button = new Button("获取会员信息");
	     getassoc_button.addListener(Events.Select, this);
	    fieldSet_isas.add(getassoc_button, td_isas); 
	    fieldSet_isas.add( new Label("电话:"), td_isas);
	    fieldSet_isas.add(s_mobil, td_isas); 
	    fieldSet_isas.add( new Label("会员卡余额:"), td_isas);
	    fieldSet_isas.add(html,td_isas);
	    html.setEnabled(false);
	    fieldSet_isas.add(ccode, td_isas); 
	    
	    
	    s_cardnothis.addListener(Events.KeyUp, this);
	    cname_t.addListener(Events.KeyUp, this);
	    s_mobil.addListener(Events.KeyUp, this);
	   
	    //----------------------------------------------end		
	//	bfp.add(check_isas,bfp_td_set);
	    bfp.add(fieldSet_isas,bfp_td_set);
	
		//bfp.add(html,bfp_td);
//		 aslabel = new Label("会员卡号:");
//		bfp.add(aslabel,bfp_td);
//		bfp.add(s_cardno,bfp_td);
		
		
		
//		bfp.add(new Label("客户名:"),bfp_td);
//		bfp.add(ccode,bfp_td);
		bfp.add(new Label("人数:"),bfp_td);
		bfp.add(peoplenum,bfp_td);
		bfp.add(new Label("房间号:"),bfp_td);
		bfp.add(roomno,bfp_td);
//		bfp.add(new Label("开始服务时间:"),bfp_td);
//		bfp.add(servicestartdate,bfp_td);
		//bfp.add(new Label("结束服务时间:"),bfp_td);
	
		bfp.add(new Label("应收款:"),bfp_td);
		bfp.add(Accounts,bfp_td);
		bfp.add(new Label("实收款:"),bfp_td);
		bfp.add(Paid,bfp_td);
	
		bfp.add(serviceenddate,bfp_td);
		bfp.add(createdate,bfp_td);
		//bfp.add(createdate);
		bfp.inited();
		/*
		 * leftpanel.add(codename); rightpanel.add(ccode);
		 * leftpanel.add(codename); rightpanel.add(ccode);
		 */

//		leftpanel.inited();
//		rightpanel.inited();
	
		
		TableData td = new TableData();
		td.setColspan(1);
		td.setRowspan(1);
		// 对齐方式
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);

		FieldSet fieldSet = new FieldSet();
		//fieldSet.setHeading("");
		
		fieldSet.setLayout(new TableLayout(1));
//		fieldSet.add(leftpanel, td);
//		fieldSet.add(rightpanel, td);
//		
		fieldSet.add(bfp, td);
		
//
	//	fieldSet.add(hpanel, td);

		final BorderLayout layout = new BorderLayout();
		service_panel.setHeaderVisible(false);
		service_panel.setLayout(layout);
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 0, 5, 0));
		service_panel.add(fieldSet, centerData);
		// panel.setAutoHeight(true);
		service_panel.setHeight("250");
		service_panel.setWidth("600");
		//service_panel.setAutoWidth(true);
		//panel.setEnabled(false);
		//panel.mask("", "base-x-item-disabled");
		//加入事件侦听
		ccode.addListener(Events.Change, this);
		s_cardnothis.addListener(Events.Change, this);
		//zgw check_isas.addListener(Events.Change, this);
		cname_t.addListener(Events.Change, this);
		s_mobil.addListener(Events.Change, this);
		return service_panel;

	}

	/**
	 * 服务类型
	 * 
	 * @return
	 */
	protected BaseGrid createServiceItem() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		
		// ---------------
		List<ColumnConfig> bocodecolumns = new ArrayList<ColumnConfig>();
		ColumnConfig service = new ColumnConfig("service", "服务类型", 120);

		bocodecolumns.add(service);

		ColumnConfig price = new ColumnConfig("price", "单价", 100);
		price.setAlignment(HorizontalAlignment.RIGHT);
		price.setNumberFormat(NumberFormat.getCurrencyFormat());

		bocodecolumns.add(price);
		ColumnConfig priceunit = new ColumnConfig("priceunit", "单位", 50);

		bocodecolumns.add(priceunit);

		ModelType combotype = new ModelType();
		combotype.setRoot("records");
		combotype.setRecordName("record");
		combotype.setTotalName("totle");
		// combotype.addField("code", "service");
		combotype.addField("service");
		combotype.addField("priceunit");
		combotype.addField("price");
	//	combotype.addField("bcode");
		StoreInfo comboinfo = new StoreInfo();
		comboinfo.setTablename("s_service");
		comboinfo.setSql("select service,priceunit,price from s_service where service is not null and priceunit is not null and price>0");
		comboinfo.setForcenolimit("true");
		BaseGridEditor bge = new BaseGridEditor(bocodecolumns, combotype,
				comboinfo, "service");
		

		ColumnConfig serviceitem = new ColumnConfig("serviceitem", "服务类型", 80);
		//TextField<String> serviceitemtext = new TextField<String>();
		GridEditor grideditor = new GridEditor(bge);
		serviceitem.setEditor(grideditor);
		serviceitem.setRenderer(grideditor);
		grideditor.getField().setName("serviceitem");
		grideditor.getField().addListener(Events.Change, this);
		// serviceitem.setEditor(editor);
		// serviceitem.setEditor(new CellEditor(dateField));
		// serviceitem.setDateTimeFormat(DateTimeFormat.getMediumDateFormat());
		
		ColumnConfig method = new ColumnConfig("method", "指定方式", 80);
		CellEditorFactory  cef = new CellEditorFactory();
		CellEditor ce1  = cef.getMethodCellEditor();
		method.setEditor(ce1);
		
		
		

		ColumnConfig qty = new ColumnConfig("qty", "数量", 50);
		qty.setAlignment(HorizontalAlignment.RIGHT);
		//qty.setNumberFormat(NumberFormat.getFormat("#,##0"));
		NumberField n = new NumberField();
		n.setFormat(NumberFormat.getFormat("#,##0"));
		n.setPropertyEditorType(Integer.class);
		n.setName("qty");
		n.addListener(Events.Change, this);
		qty.setEditor(new CellEditor(n));
		//qty.getEditor().addListener(Events.Complete, this);
		
		ColumnConfig upric = new ColumnConfig("upric", "单价", 80);
		// TextField<String> uprictext = new TextField<String>();
		// upric.setEditor(new CellEditor(uprictext));
		upric.setAlignment(HorizontalAlignment.RIGHT);
		upric.setNumberFormat(NumberFormat.getCurrencyFormat());
		upric.setEditor(new CellEditor(new NumberField()));
		upric.getEditor().getField().setName("upric");
		upric.getEditor().getField().addListener(Events.Change, this);
		//upric.getEditor().addListener(Events.Complete, this);
	
		ColumnConfig qtyunit = new ColumnConfig("qtyunit", "单位", 50);
		TextField<String> qtyunittext = new TextField<String>();
		qtyunit.setEditor(new CellEditor(qtyunittext));
		
		
		ColumnConfig total = new ColumnConfig("total", "小计", 80);
		// TextField<String> uprictext = new TextField<String>();
		// upric.setEditor(new CellEditor(uprictext));
		total.setAlignment(HorizontalAlignment.RIGHT);
		total.setNumberFormat(NumberFormat.getCurrencyFormat());
		//total.setEditor(new CellEditor(new NumberField()));
		
		
	/*	total.getEditor().addListener(Events.Complete, new Listener(){

			@Override
			public void handleEvent(BaseEvent be) {
				// TODO Auto-generated method stub
				Debug.printf("editorchange..", 4);
			}
			
		});*/
		
		/*total.setRenderer(new GridCellRenderer<BusiObject>() {  
	        public String render(BusiObject model, String property, ColumnData config, int rowIndex,  
	            int colIndex, ListStore<BusiObject> store, Grid<BusiObject> grid) {  
	        	BusiObject task = (BusiObject) model;  
	        	double d1 = Double.parseDouble(task.get("upric")==null?"0":task.get("upric").toString());
	        	double d2 = Double.parseDouble(task.get("qty")==null?"0":task.get("qty").toString());
	          return NumberFormat.getCurrencyFormat().format(  
	        		  d1* d2);  
	        }  
	      });  */
		
		
		
//		 bcode = new ColumnConfig("bcode", "技师", 100);

		 //*******************************
		 
//		 List<ColumnConfig> bocodecolumns2 = new ArrayList<ColumnConfig>();
//			ColumnConfig bcode2 = new ColumnConfig("bcode", "人员", 100);
//
//			bocodecolumns2.add(bcode2);
//
//			ColumnConfig bname = new ColumnConfig("bname", "人员名", 50);
//
//			bocodecolumns2.add(bname);
//		 BaseGridEditor bge2 = new BaseGridEditor(bocodecolumns2,bcode_rander_store, "bcode");
//			
//			
//
//			GridEditor grideditor2 = new GridEditor(bge2,"bcode","bname");
//			bcode.setEditor(grideditor2);
//			bcode.setRenderer(grideditor2);
		
			
			
		
		 //**********************************
			ColumnConfig c9 = new ColumnConfig("bcodeshortcode", "技师编号", 70);
			TextField<String> c9text = new TextField<String>();
			c9.setEditor(new CellEditor(c9text));
			c9text.setName("bcodeshortcode");
			c9text.addListener(Events.Change, this);
			
			c9text.setFireChangeEventOnSetValue(true);
			
			
			//c9.getEditor().getField().addListener(Events.Change, this);
			
			ColumnConfig c10 = new ColumnConfig("bname", "技师名", 80);
			TextField<String> c10text = new TextField<String>();
			c10.setEditor(new CellEditor(c10text));
			c10text.setName("bname");
//		columns.add(bcode);
			columns.add(c9);
			columns.add(c10);
		columns.add(method);
		columns.add(serviceitem);
		
		columns.add(qty);
		columns.add(upric);
		columns.add(qtyunit);
		columns.add(total);
		qtyunit.getEditor().addListener(Events.Complete, this);
		upric.getEditor().addListener(Events.Complete, this);
		
		
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("idx");
		type.addField("icode");

		type.addField("serviceitem");

		type.addField("qty");

		type.addField("qtyunit");
		type.addField("upric");
		type.addField("bcode");
		type.addField("method");
		type.addField("total");
		type.addField("bcodeshortcode");
		type.addField("bname");
		type.getField("qty").setType(Integer.class);
		type.getField("total").setType(Double.class);
		type.getField("upric").setType(Double.class);
		type.getField("bcode").setType(String.class);
		StoreInfo info = new StoreInfo();

		info.setTablename("s_service_po_g");
		info.setSql("select * from s_service_po_g where upicode='{icode}'");
		info.setParent(store);// 关联主表
		// info.setAutoinccolumn("idx"); // 自增字段，如果子表一定需要
		info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "upicode" });// 关联主表字段
																		// (子表)

		info.setKeyname(new String[] { "icode" }); // 主键

		s_service_po_g_store = new BaseStore<BusiObject>(type, info);
		s_service_po_g_store.setName("s_service_po_g");
		
		/*s_service_po_g_store.addBaseStoreListener(BaseStoreEvent.EventType.CELL_CHANGED, new BaseStoreListener(){

			@Override
			public void BaseStoreEventHandle(BaseStoreEvent evt) {
				
				Debug.printf("name ="+evt.getCellname(),	4);
				Debug.printf("index ===="+evt.getCurrowindex(),	4);
				//Debug.printf("class ="+ evt.getSource().getClass(), 4);
				//evt.getBasestore()
				Debug.printf("value =="+(evt.getBasestore().getValue(evt.getCurrowindex(), evt.getCellname())),	3);
			}

			@Override
			public void onBrowserEvent(Event event) {
				
				
			}
			
		} );*/
		/*s_service_po_g_store.addStoreListener(new StoreListener<BusiObject>(){
			 public void storeUpdate(StoreEvent<BusiObject> se) {
				 Debug.printf("se=="+se.getIndex(),	4);
				 Debug.printf("se=="+se.getSource(),	4);
				 Debug.printf("se=="+se.,	4);
				 Debug.printf("se=="+se.getModel().get("qty"),	4);
			  }
		});*/
		ColumnModel cm = new ColumnModel(columns);

		AggregationRowConfig averages = new AggregationRowConfig();
		averages.setHtml("serviceitem", "合计：");

		// with summary type and format
//		averages.setSummaryType("upric", SummaryType.MAX);
//		 averages.setSummaryFormat("upric", NumberFormat.getCurrencyFormat());

		// with renderer
//		 averages.setSummaryType("qty", SummaryType.SUM);
//		
//		  averages.setRenderer("qty", new AggregationRenderer() { 
//			  public Object  render(Number value, int colIndex, Grid grid, ListStore store)
//			  { 
//			  
//				  // you can return html here
//				  Debug.printf(value, 4);
//				  if(value!=null)
//					  return number.format(value.doubleValue());
//				  else
//					  return number.format(new Double("0"));
//			  }
//		  });
		averages.setSummaryType("total", SummaryType.SUM);
		averages.setSummaryFormat("total", NumberFormat.getCurrencyFormat());
		
		 cm.addAggregationRow(averages);

		bge.setTarget_store(s_service_po_g_store);
		bge.setSourcecol(new ColumnInfo[] { new ColumnInfo("priceunit",ColumnInfo.STRING), new ColumnInfo("price",ColumnInfo.DOUBLE) });
		bge.setTarget_col(new ColumnInfo[] { new ColumnInfo("qtyunit",ColumnInfo.STRING), new ColumnInfo("upric",ColumnInfo.DOUBLE) });
		//		
		 s_service_po_g_grid = new BaseGrid(s_service_po_g_store, cm)
		 {
			 public void deleterecord(){
				 super.deleterecord();
				 trysave();
				 cal();
			 }
		 };
		// s_service_po_g_grid.setEnabled(false);
		// s_service_po_g_grid.mask("sss", "base-x-item-disabled");
		// GroupSummaryView summary = new GroupSummaryView();
		cbs.setSelectionMode(SelectionMode.SINGLE);
		
		s_service_po_g_grid.setSelectionModel(cbs);
//		s_service_po_g_grid.setAutoHeight(true);

		//s_service_po_g_grid.setAutoWidth(true);
		s_service_po_g_grid.setWidth("600");
		s_service_po_g_grid.setHeight(195);
	

		registerStore(s_service_po_g_store);
		// bcgp = new
		// BaseChileGridPanel(type,info,columns,BaseChileGridPanel.QUERY_MODE);
		//s_service_po_g_store.addBaseStoreListener(BaseStoreEvent.EventType.CELL_CHANGED, this);
		s_service_po_g_grid.addListener(Events.AfterEdit, this );
		return s_service_po_g_grid;
	}
void initListener()
{
	StoreListener<ModelData> listener = new StoreListener<ModelData>() {
		 public void storeDataChanged(StoreEvent<ModelData> se) {
		       // onDataChanged(se);
		      }
	 };
}
 int sync = 0;
	/* (non-Javadoc)
	 * @see com.extjs.gxt.ui.client.event.Listener#handleEvent(com.extjs.gxt.ui.client.event.BaseEvent)
	 */
	public void handleEvent(BaseEvent be) {
		
		sync++;
		try {
			if (sync == 1) {

				
				Debug.printf(be.getType(), 4);
				if (be.getType() == Events.Select) {
					if (be.getSource() == startbutton) {
						Date d = new Date();
						/*
						 * DateTimeFormat activeFormat =
						 * DateTimeFormat.getFormat("yyyy-M-d h:mm:ss"); String
						 * formattedValue = activeFormat.format(d);
						 */
						servicestartdate.setFireChangeEventOnSetValue(true);
						servicestartdate.setValue(d);
						// servicestartdate.setRawValue(formattedValue);
					} else if (be.getSource() == endbutton) {
						Date d = new Date();
						/*
						 * DateTimeFormat activeFormat =
						 * DateTimeFormat.getFormat("yyyy-M-d h:mm:ss"); String
						 * formattedValue = activeFormat.format(d);
						 */
						serviceenddate.setFireChangeEventOnSetValue(true);
						serviceenddate.setValue(d);
					} else if (be.getSource() == paidbutton) {
						
						//检查是否服务类型有必录项没有录入
						if(!canbepaid())
						{
							MessageBox.alert("警告", "服务类型数量没有录入", null);
						}
						else if(bfp.isValid())
						
						{
//							check_isas Paid
							if(/* zgw check_isas.getValue()==true &&*/ Paid.getValue().doubleValue()>0)
							{
								//if(associator==null)
								if(s_cardnothis.getValue()==null || s_cardnothis.getValue().equals(""))
								{
									MessageBox.alert("警告", "请录入会员卡号",null);
									return;
								}
								//if(associator.getFinance()>Paid.getValue().doubleValue())
								if(html.getValue()!=null && html.getValue().doubleValue()>Paid.getValue().doubleValue())
								{
//								store.setValue("busistatus", "524288");
//								Date d = new Date();
//								serviceenddate
//										.setFireChangeEventOnSetValue(true);
//								serviceenddate.setValue(d);
//								store.trySave();
//								 setEnableed(true);
									if(Paid.getValue()!=null && store.getValue("Paid")!=null)
									{
										store.setValue("Paid", Paid.getValue());
									}
									if(store.getValue("Paid")==null )
									{
										MessageBox.info("提示", "“实收款”没有录入，请重新录入。", null);
									}
									else
									{
										paided();
									}
								}
								else 
								{
									MessageBox.alert("警告", "会员余额不能够支付本次消费。余额"+html.getValue()+"本次消费:"+Paid.getValue(),null);
								}
							}
							else if(/*zgw check_isas.getValue()==true &&*/ Paid.getValue().doubleValue()<1)
							{
								MessageBox.alert("警告", "收款金额录入不正确!",null);
								
							}
//	zgw						else if(check_isas.getValue()==false)
//							{
////								store.setValue("busistatus", "524288");
////								Date d = new Date();
////								serviceenddate
////										.setFireChangeEventOnSetValue(true);
////								serviceenddate.setValue(d);
////								store.trySave();
////								 setEnableed(true);
//								paided();
//							}
						}
						else{
							MessageBox.alert("错误","有内容没有录入.操作失败", null);
						}
						// bcode.setRenderer(new GridCellRenderer<BusiObject>()
						// {
						// public String render(BusiObject model, String
						// property, ColumnData config, int rowIndex,
						// int colIndex, ListStore<BusiObject> store,
						// Grid<BusiObject> grid) {
						//			        	
						//			        	
						// return "zzzs";
						// }
						// });
					}
					else if (be.getSource() == printer) {
						
//						store.setValue("busistatus", "128");
//						store.setValue("busistatus", "524288");
						Object status = store.getValue("busistatus");
						if(status!=null && status.equals("524288"))
						{
						Print() ;
						}
						else
						{
							MessageBox.alert("警告", "必须先进行“收银”才能进行“打印”。\n“收银”按钮在工具栏。", null);
						}
					}
					else if (be.getSource() == addnew) {
						
						basegrid.addnewrecord();
					}
					
					else if (be.getSource() == getassoc_button) {
						if(!(s_cardnothis.getValue()==null || s_cardnothis.getValue().equals("")) )
						{
						final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
						.create(com.base.myproject.client.tools.GreetingService.class);
						greetingService.getAssociator(s_cardnothis.getValue(),4,this);
						}
						else if(!(cname_t.getValue()==null || cname_t.getValue().equals("")) )
						{
						final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
						.create(com.base.myproject.client.tools.GreetingService.class);
						greetingService.getAssociator(cname_t.getValue(),2,this);
						}
						else if(!(s_mobil.getValue()==null || s_mobil.getValue().equals("")) )
						{
						final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
						.create(com.base.myproject.client.tools.GreetingService.class);
						greetingService.getAssociator(s_mobil.getValue(),3,this);
						}
					}
					else if (be.getSource() == associatorbutton) {
						AssociatorPanel associatorpanel = new AssociatorPanel();
						associatorpanel.getDialog().show();
					}
					else if (be.getSource() == changedpo) {
						
						BcodeChecker b = new BcodeChecker(){

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub
								
							}

							@Override
							public void onSuccess(Boolean result) {
								Debug.printf(result.toString());
								GWT.log("result", null);
								if(result)
								{
									
									store.setValue("busistatus", "128");
									vpanel.unmask();
									
								}
								else
								{
									Info.display("","密码不正确!!");
									
								}
							}
							
						};
				
						b.showDialog();
					
						
					}
					
					else if (be.getSource() == addfinance) {
						//bug
						AddFinace addfinace = new AddFinace()
						{
							public void addfinancesuccess(Element elem ) {
								super.addfinancesuccess(elem);
								if(check_isas.getValue()!=null && check_isas.getValue() && s_cardno.getValue()!=null)
								{
								final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
								.create(com.base.myproject.client.tools.GreetingService.class);
								greetingService.getAssociator(s_cardno.getValue(),4,new AsyncCallback<Associator> (){

									@Override
									public void onFailure(Throwable caught) {
										// TODO Auto-generated method stub
										
									}

									@Override
									public void onSuccess(Associator result) {
									//	if(associator.getS_cardno().equals(result.getS_cardno()))
										GWT.log("---"+s_cardnothis.getValue(), null);
										GWT.log("==="+result.getS_cardno(), null);
										System.out.println("---"+s_cardnothis.getValue());
										System.out.println("==="+result.getS_cardno());
										if(s_cardnothis.getValue().equals(result.getS_cardno()))
										getAssociatorSuccess(result);
									}
									
								});
								}
							}
						};
						addfinace.showDialog();
						
//						new AddFinace().showDialog();
					}
					//交接
					else if (be.getSource() == associate) {
						printer();
					}
					//添加新会员
					else if(be.getSource().equals(newccode))
					{
						 NewCCode newccode = new NewCCode();
						 newccode.show();
					}
					
				} else if (be.getType() == Events.Change) {
					Debug.printf("changed!!", 4);
					// String name = (be.getSource()).getField().getName();
					Debug.printf("changed!!"
							+ ((FieldEvent) be).getField().getName(), 4);
					String name = ((FieldEvent) be).getField().getName();
					if (name.equals("qty")) {
						
					} else if (name.equals("upric")) {
					

					}
					else if (name.equals("bcodeshortcode")) {}
					
					// 录入卡号
					else if (name.equals("s_cardno")) {
						Debug.printf("s_cardno input ", 4);

						be.setCancelled(true);
						final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
						.create(com.base.myproject.client.tools.GreetingService.class);
						greetingService.getAssociator(s_cardnothis.getValue(),4,this);

					} 
					else if (name.equals("cname")) {
						Debug.printf("cname input ", 4);

//						be.setCancelled(true);
//						final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
//						.create(com.base.myproject.client.tools.GreetingService.class);
//						greetingService.getAssociator(cname_t.getValue(),2,this);

					}
					else if (name.equals("s_mobil")) {
						Debug.printf("s_mobil input ", 4);
//
//						be.setCancelled(true);
//						final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
//						.create(com.base.myproject.client.tools.GreetingService.class);
//						greetingService.getAssociator(s_mobil.getValue(),3,this);

					}
					
					else if (name.equals("ccode")) {
//						Debug.printf("ccode input ", 4);
//						ModelData md = CodeNameMapFactory
//								.getBaseccodestore()
//								.findModel("code", ccode.getValue().get("code"));
//						if (md != null) {
//							s_cardno.setFireChangeEventOnSetValue(true);
//
//							s_cardno.setValue(md.get("s_cardno") == null ? ""
//									: md.get("s_cardno").toString());
//							s_cardno.setFireChangeEventOnSetValue(false);
//						} else {
//							s_cardno.setFireChangeEventOnSetValue(true);
//
//							s_cardno.setValue("");
//							s_cardno.setFireChangeEventOnSetValue(false);
//						}
					
					}
//					else if (name.equals("check_isas")) {
//						
//						Debug.printf("check_isas ="+check_isas+" value="+check_isas.getValue());
//						if(check_isas.getValue()!=null && check_isas.getValue())
//						{
//							s_cardnothis.setEnabled(true);
//							ccode.setEnabled(false);
//							//aslabel.setVisible(true);
//							Debug.printf("enable");
//							//bfp.layout();
//							fieldSet_isas.setExpanded(true);
//						}
//						else{
//							
//							s_cardnothis.setEnabled(false);
//							ccode.setEnabled(false);
//							//aslabel.setVisible(false);
//							
//							//bfp.layout();
//							reset();
//							//associator = null;
//							fieldSet_isas.setExpanded(false);
//						}
//						
//					}

				} else if (be.getType() == Events.Complete) {
					/*
					 * Debug.printf("resource=="+((Editor)be.getSource()).getTitle
					 * (), 4);
					 * Debug.printf("Editor value=="+((Editor)be.getSource
					 * ()).getValue(), 4);
					 * Debug.printf("Editor getName=="+((Editor
					 * )be.getSource()).getField().getName(), 4);
					 * Debug.printf(s_service_po_g_store.getValue("qty"), 4);
					 */
					// com.extjs.gxt.samples.resources.client.model.Stock;
					// String name =
					// ((Editor)be.getSource()).getField().getName();
					// if (name.equals("qty")||name.equals("upric"))
					{
						// int index = s_service_po_g_store.getCurcowindex();
						/*
						 * if (index != -1) { double qty =
						 * Double.parseDouble(s_service_po_g_store
						 * .getValue(index, "qty") == null ? "0" :
						 * s_service_po_g_store.getValue(index, "qty")
						 * .toString()); double price =
						 * Double.parseDouble(s_service_po_g_store
						 * .getValue(index, "upric") == null ? "0" :
						 * s_service_po_g_store.getValue(index, "upric")
						 * .toString()); // total Debug.printf("qty =" + qty,
						 * 4); Debug.printf("price = " + price, 4);
						 * s_service_po_g_store.setValue(index, "total", qty *
						 * price); }
						 */
					}
				}
				else if (be.getType() == Events.KeyUp) {
					Debug.printf("keyup" +be.getClass());
					Debug.printf("keyup" +be.getType());
					if(be instanceof ComponentEvent)
					{
						Debug.printf("keyup" +((ComponentEvent) be).getKeyCode());
						if(((ComponentEvent) be).getKeyCode()==9)
						{
							
						}
					
						else if(be.getSource().equals(s_cardnothis))
					{
						
							ccode.setFireChangeEventOnSetValue(true);
							cname_t.setFireChangeEventOnSetValue(true);
							s_mobil.setFireChangeEventOnSetValue(true);
							//s_cardnothis.setFireChangeEventOnSetValue(true);
							html.setFireChangeEventOnSetValue(true);
							//s_cardnothis.setValue("");
							ccode.setValue("");
							html.setValue(0);
							
							
							cname_t.setValue("");
							s_mobil.setValue("");
							
							//s_cardnothis.setFireChangeEventOnSetValue(false);
							ccode.setFireChangeEventOnSetValue(false);
							cname_t.setFireChangeEventOnSetValue(false);
							s_mobil.setFireChangeEventOnSetValue(false);
							html.setFireChangeEventOnSetValue(false);
					}
					else if(be.getSource().equals(s_mobil))
					{
						s_cardnothis.setValue("");
						cname_t.setValue("");
						html.setValue(0);
					}
					else if(be.getSource().equals(cname_t))
					{
						s_mobil.setValue("");
						s_cardnothis.setValue("");
						html.setValue(0);
					}
					}
				}
				else if (be.getType() == Events.AfterEdit) {
					Debug.printf("ddd");
					final GridEvent<BusiObject> be2 = (GridEvent<BusiObject>)be;
					//((BaseGrid<BusiObject>)be2.getGrid()).setValue(be2.getRowIndex(), "bname", "sss");
					

					
					
					if (be2.getProperty().equals("bcodeshortcode")) {
						final int index = be2.getRowIndex();
						Debug.printf("index = " + index);
						if (index != -1) {

							// Object shortcode = ((FieldEvent)
							// be).getField().getValue();
							Object shortcode = be2.getValue();
							if (shortcode != null) {
								String shortcodestr = shortcode.toString();
								final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
										.create(com.base.myproject.client.tools.GreetingService.class);
							//	greetingService.getbnamebyshortname(shortcodestr, new AsyncCallback<String>() {
								greetingService.getBcode("bcodeshortcode",shortcodestr, new AsyncCallback<BcodeSync>() {
								
									@Override
									public void onFailure(Throwable caught) {

									}

									@Override
									public void onSuccess(BcodeSync result) {
										if (result != null) {
											Debug.printf("index2 = " + result.getBname()+""+result.getBcodeshortcode());
//											s_service_po_g_store.setValue(index,
//													"bcode", result[0]);
//											s_service_po_g_store.setValue(index,
//													"bname", result[1]);
											
									//		((BaseGrid<BusiObject>)be2.getGrid()).setValue(be2.getRowIndex(), "bcode", result[0]);
											((BaseGrid<BusiObject>)be2.getGrid()).setValue(be2.getRowIndex(), "bname", result.getBname());
											
										}
									}

								});
							}
						}
					
				}
					else if (be2.getProperty().equals("qty") || be2.getProperty().equals("upric") ||be2.getProperty().equals("serviceitem")) {

						final int index = be2.getRowIndex();
						if (index != -1) {
							double qty = Double.parseDouble(((BaseGrid<BusiObject>)be2.getGrid()).getValue(index, "qty") == null ? "0"
									: ((BaseGrid<BusiObject>)be2.getGrid()).getValue(index, "qty").toString());
							
							double price = Double
									.parseDouble(((BaseGrid<BusiObject>)be2.getGrid()).getValue(index, "upric") == null ? "0"
											: ((BaseGrid<BusiObject>)be2.getGrid()).getValue(index, "upric").toString());
							// total

							((BaseGrid<BusiObject>)be2.getGrid()).setValue(index, "total", qty
									* price);

							cal();
						}

					
					}
				
//						else if (be2.getProperty().equals("upric")) {
//							final int index = be2.getRowIndex();
//							if (index != -1) {
//								double upric = Double.parseDouble(((FieldEvent) be)
//										.getField().getValue() == null ? "0"
//										: ((FieldEvent) be).getField().getValue()
//												.toString());
//								double qty = Double
//										.parseDouble(s_service_po_g_store.getValue(
//												index, "qty") == null ? "0"
//												: s_service_po_g_store.getValue(
//														index, "qty").toString());
//								// total
//
//								s_service_po_g_store.setValue(index, "total", qty
//										* upric);
//							}
//					}
					
				}
				
			}
			}
		
			finally {
			sync--;
			Debug.printf("suync="+sync, 4);
		}
		
	}

	protected void cal() {
		double d = 0.00;
		for (int i = 0; i < s_service_po_g_store.getCount(); i++) {
			if((s_service_po_g_store.getAt(i).getBusistatus() & BusiObject.DELETE)!=BusiObject.DELETE)
			d += Double.parseDouble(s_service_po_g_store
					.getValue(i, "total").toString());
		}
		Accounts.setFireChangeEventOnSetValue(true);
		Accounts.setValue(d);
		
		Paid.setFireChangeEventOnSetValue(true);
		Paid.setValue(d);
		
	}
	
	public void paided()
	{
		store.setValue("busistatus", "524288");
		Date d = new Date();
		serviceenddate
				.setFireChangeEventOnSetValue(true);
		serviceenddate.setValue(d);
		
		store.trySave();
		 setEnableed(true);
		
	}

	void updateassociator(Associator result)
	{
		
		//this.associator = result;
		s_cardnothis.setValue(result.getS_cardno());
		
		ccode.setValue(result.getCcode());
		cname_t.setValue(result.getName());
		s_mobil.setValue(result.getPhone());
		html.setValue(result.getFinance());
	}
	@Override
	public void BaseStoreEventHandle(BaseStoreEvent evt) {
		Debug.printf("icode="+evt.getBasestore().getValue("icode"),4);
		Debug.printf("evt.getEventType()"+evt.getEventType());
		Object status = evt.getBasestore().getValue("busistatus");
		if(evt.getEventType()==BaseStoreEvent.EventType.ROW_CHANGED)
		{
		
		if(status!=null && status.equals("524288"))
		{
			//service_panel.setBodyStyleName("base-x-panel-body-disabled");
			
	
			 setEnableed(true);
			//s_service_po_g_grid.mask(null,"base-ext-el-mask");
		}
		else
		{
			//service_panel.setBodyStyleName("x-panel-body");
			//service_panel.setEnabled(true);
			 
			 setEnableed(false);
			 //s_service_po_g_grid.unmask();
		}
		}
		else if(evt.getEventType()==BaseStoreEvent.EventType.CELL_CHANGED)
		{}
	}

	protected void setEnableed(boolean b)
	{
		if(b)
		{
			 vpanel.mask(null,"base-ext-el-mask");
			 changedpo.setEnabled(true);
		}
		else
		{
			 vpanel.unmask();
			 changedpo.setEnabled(false);
		}
	}
	
	
	@Override
	public void onFailure(Throwable caught) {
		
		Debug.printf("error");
	}

	@Override
	public void onSuccess(Associator result) {
		getAssociatorSuccess(result);
	}
	protected void  getAssociatorSuccess(Associator result)
			{
		try
		{
			sync++;
		if(result.getName()!=null)
		{
//		ccode.setSimpleValue(result.getCcode());
		ccode.setFireChangeEventOnSetValue(true);
		cname_t.setFireChangeEventOnSetValue(true);
		s_mobil.setFireChangeEventOnSetValue(true);
		s_cardnothis.setFireChangeEventOnSetValue(true);
		html.setFireChangeEventOnSetValue(true);
		updateassociator(result);

		s_cardnothis.setFireChangeEventOnSetValue(false);
		ccode.setFireChangeEventOnSetValue(false);
		cname_t.setFireChangeEventOnSetValue(false);
		s_mobil.setFireChangeEventOnSetValue(false);
		html.setFireChangeEventOnSetValue(false);
		//html.setHtml("可用余额："+result.getFinance());
		
		// associator = result;
		}
		else{
//			ccode.setFireChangeEventOnSetValue(true);
//			cname_t.setFireChangeEventOnSetValue(true);
//			s_mobil.setFireChangeEventOnSetValue(true);
//			s_cardnothis.setFireChangeEventOnSetValue(true);
//			html.setFireChangeEventOnSetValue(true);
			reset();
			
			//html.setValue(0);
			
//			s_cardnothis.setFireChangeEventOnSetValue(false);
//			ccode.setFireChangeEventOnSetValue(false);
//			cname_t.setFireChangeEventOnSetValue(false);
//			s_mobil.setFireChangeEventOnSetValue(false);
//			html.setFireChangeEventOnSetValue(false);
			Info.display("错误", "没要找到会员信息！");
		}
		}
		finally {
			sync--;
			Debug.printf("suync2="+sync, 4);
		}
			}
	private void reset()
	{
		ccode.setFireChangeEventOnSetValue(true);
		cname_t.setFireChangeEventOnSetValue(true);
		s_mobil.setFireChangeEventOnSetValue(true);
		s_cardnothis.setFireChangeEventOnSetValue(true);
		html.setFireChangeEventOnSetValue(true);
		s_cardnothis.setValue("");
		ccode.setValue("");
		html.setValue(0);
		
		
		cname_t.setValue("");
		s_mobil.setValue("");
		
		s_cardnothis.setFireChangeEventOnSetValue(false);
		ccode.setFireChangeEventOnSetValue(false);
		cname_t.setFireChangeEventOnSetValue(false);
		s_mobil.setFireChangeEventOnSetValue(false);
		html.setFireChangeEventOnSetValue(false);
	}
	
	
	//检查必录
	boolean canbepaid()
	{
		for(BusiObject bo:s_service_po_g_store.getModels())
		{
			if(bo.get("qty")==null || bo.get("qty").equals(""))
				return false;
		}
		return true;
	}
	/*
	@Override
	public void Print() { 
		String cardno = "";
//		if(s_cardno.getValue()==null || s_cardno.getValue()==""){
//			cardno = "&nbsp;";
//		}
		
		DateTimeFormat d = DateTimeFormat.getFormat("yyyy-M-d h:mm:ss");

		String str = "";
		str += "<table>";
		str += "<tr>";
		str += "<td width=\"120\"><span>会员卡号:</span></td><td>"+(s_cardno.getValue()==null?"无":s_cardno.getValue())+"</td>";
		str += "</tr>";
		
		str += "<tr>";
		str += "<td width=\"120\"><span>客户名:</span></td><td>"+ccode.getValue()+"</td>";
		str += "</tr>";
 
		str += "<tr>";
		str += "<td width=\"120\"><span>人数:</span></td><td>"+(peoplenum.getValue()==null?"1":peoplenum.getValue().intValue())+"</td>";
		str += "</tr>";
		
		str += "<tr>";
		str += "<td width=\"120\"><span>房间号:</span></td><td>"+roomno.getValue()+"</td>";
		str += "</tr>";
		
//		str += "<tr>";
//		str += "<td width=\"120\"><span>开始服务时间:</span></td><td><span class=\"dateFormat\">"+d.format(servicestartdate.getValue())+"</span></td>";
//		str += "</tr>";
//		
//		str += "<tr>";
//		str += "<td width=\"120\"><span>结束服务时间:</span></td><td><span class=\"dateFormat\">"+d.format(serviceenddate.getValue())+"</span></td>";
//		str += "</tr>";
	 
		str += "<tr>";
		str += "<td width=\"120\"><span>应收款:</span></td><td>"+Accounts.getValue()+"</td>";
		str += "</tr>";
		
		str += "<tr>";
		str += "<td width=\"120\"><span>实收款:</span></td><td>"+Paid.getValue()+"</td>";
		str += "</tr>";

		str += "</table>";

		PrintDialog print = new PrintDialog("", "/gwtbase/css/print.css", str);
		
		
	}
	*/
	@Override
	public void Print() { 
		String cardno = "";
		if(s_cardnothis.getValue()==null || s_cardnothis.getValue()==""){
			cardno = "&nbsp;";
		}
		
		DateTimeFormat d = DateTimeFormat.getFormat("yyyy-M-d H:mm:ss");

		String str = "";
		str += "<table cellpadding=\"3\">";
		str += "<tr>";
		str += "<td colspan=\"2\" align=\"center\"><span><U><strong>"+SystemInfo.getInstance().getTitle()+"</strong></U></span></td>";
		str += "</tr>";
		str += "<tr>";
		str += "<td width=\"180\"><span>卡号:</span>"+s_cardnothis.getValue()+"</td>";
		str += "<td><span>顾客:</span>"+cname_t.getValue()+"</td>";
		str += "</tr>";
 
		str += "<tr>";
		str += "<td colspan=\"2\" align=\"left\"><span>人数:</span>"+(peoplenum.getValue()==null?"1":peoplenum.getValue().intValue())+"</td>";
		
		str += "</tr>";
		str += "<tr>";
		
		
		double dd = 0;
		if(html.getValue()!=null)
		{
			dd=html.getValue().doubleValue()-Paid.getValue().doubleValue();
		}
		str += "<td colspan=\"2\" align=\"left\"><span>会员卡余额:</span>"+(dd)+"</td>";
		str += "</tr>";
//		str += "<tr>";
//		str += "<td colspan=\"2\"><span>开始时间:</span><span class=\"dateFormat\">"+d.format(servicestartdate.getValue())+"</span></td>";
//		str += "</tr>";
//		
		str += "<tr>";
		str += "<td colspan=\"2\"><span>时间:</span><span class=\"dateFormat\">"+d.format(createdate.getValue())+"</span></td>";
		str += "</tr>";
		str += "<tr>";
		str += "<td colspan=\"2\"><span>操作员:</span><span >"+Env.getEnvname()+"</span></td>";
		str += "</tr>";
//	 
		//---
		str += "<tr>";
		str += "<td colspan=\"2\">";
		
		str += "<table class=\"tblprice\">";
		
		//标题
		str += "<tr style=\"\">";
		str += "<th width=\"250\">名称</th>";
		str += "<th width=\"120\">数量</th>";
		str += "<th>金额</th>";
		str += "</tr>";
		
		//循环
//		str += "<tr>";
//		str += "<td>洗发水</td><td>2</td><td width=\"200\">20</td>";
//		str += "</tr>";
//		
//		str += "<tr>";
//		str += "<td>洗脚水</td><td>2</td><td width=\"200\">40</td>";
//		str += "</tr>";
		for(BusiObject bu:s_service_po_g_store.getModels())
		{
			str += "<tr>";
			str += "<td>"+bu.get("serviceitem")+"</td><td>"+bu.get("qty")+"</td><td width=\"200\">"+bu.get("total")+"</td>";
			str += "</tr>";
		}
		
		
		str += "</table>";
		
		str += "</td>";
		str += "</tr>";
		
	
		//---
		
//		str += "<tr>";
//		str += "<td align=\"right\" colspan=\"3\" style=\"border-top:1px solid #000\">合计金额:60</td>";
//		str += "</tr>";
		
		str += "<tr>";
		str += "<td><span>应收款:</span>"+Accounts.getValue()+"</td>";
		str += "<td><span>实收款:</span>"+Paid.getValue()+"</td>";
		str += "</tr>";
		str += "<tr>";
		str += "<td colspan=\"2\" align=\"left\"><span>"+SystemInfo.getInstance().getName()+"</span></td>";
		
		str += "</tr>";
		
		
		str += "<tr>";
		
		str += "<td colspan=\"2\" align=\"left\" ><span>电话:</span>"+SystemInfo.getInstance().getTel()+"</td>";
		str += "</tr>";
		
		str += "<tr>";
		str += "<td colspan=\"2\" align=\"center\"><span>"+SystemInfo.getInstance().getSlogan()+"</span></td>";
		str += "</tr>";
		str += "</table>";
		PrintDialog print = new PrintDialog("", "/"+GWT.getModuleName()+"/css/print.css", str);
		
		
	}
	
	
	protected void printer()
	{
		/*
		if(s_cardno.getValue()==null || s_cardno.getValue()=="")
		{
			MessageBox.alert("错误", "没找到会员卡号，请在 “会员管理” 模块进行打印", null);
			return;
		}
		*/
		PrinterSync printersync = new PrinterSync();
		printersync.setBcode(Env.getEnvbcode());
		printersync.setPrintcode("service_1003");
		printersync.setIcode(s_cardnothis.getValue()+"");
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
		.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getPrinterHtml(printersync,new AsyncCallback<String>(){

			@Override
			public void onFailure(Throwable caught) {
				
				
			}

			@Override
			public void onSuccess(String result) {
				Debug.printf(result.toString());
				PrintDialog print = new PrintDialog("", "", result);
			}
			
		});
	}
}
